/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "配置.h"

#include <UI/UI.h>



std::map<std::u16string, S_Props> C_键位映射配置::g_全局键位映射容器;
std::map<std::u16string, S_Props> C_键位映射配置::g_3D视口键位映射容器;
std::map<std::u16string, S_Props> C_键位映射配置::g_2D视口键位映射容器;
std::map<std::u16string, S_Props> C_键位映射配置::g_节点视口键位映射容器;
std::map<std::u16string, S_Props> C_键位映射配置::g_曲线视口键位映射容器;
std::map<std::u16string, S_Props> C_键位映射配置::g_大纲视口键位映射容器;



static void f_配置_添加操作键位(C_Widget* self, S_键位映射* key) {
	switch (key->m_Type) {
		case E_键位映射类型::e_键映射类型_双击:
		case E_键位映射类型::e_键映射类型_鼠标:
		case E_键位映射类型::e_键映射类型_拖放:
		case E_键位映射类型::e_键映射类型_拖拽:
			f_ui_注册鼠标快捷键(self, (S_键位映射*)key);
			break;
		case E_键位映射类型::e_键映射类型_键盘: {
			f_ui_注册键盘快捷键(self, (S_键盘键位映射*)key);
			break;
		}
	}
}

static std::u16string f_配置_生成全局键值名称(S_键位映射* key) {
	std::u16string keyName;

	switch (key->m_Type) {
		case E_键位映射类型::e_键映射类型_键盘: {
			const S_键盘键位映射* k = (const S_键盘键位映射*)key;

			keyName += uint16((k->键盘按键[0] << 0) | (k->键盘按键[1] << 8));
			keyName += uint16((k->键盘按键[2] << 0) | (k->键盘按键[3] << 8));
			break;
		}
		case E_键位映射类型::e_键映射类型_鼠标:
		case E_键位映射类型::e_键映射类型_拖放: {
			const S_拖拽键位映射* k = (const S_拖拽键位映射*)key;

			keyName += uint16((k->键盘按键[0] << 0) | (k->键盘按键[1] << 8));
			keyName += uint16((k->键盘按键[2] << 0) | (k->键盘按键[3] << 8));

			keyName += uint16((k->鼠标按键[0] << 0) | (k->鼠标按键[1] << 8));
			keyName += uint16((k->鼠标按键[2] << 0) | (k->鼠标按键[3] << 8));
			break;
		}
		case E_键位映射类型::e_键映射类型_双击: {
			break;
		}
		default:
			break;
	}
}


S_键位映射* f_配置_取操作键位(uint32 分组) {
	
	return nullptr;
}

C_键位映射配置::C_键位映射配置() {
	
}

void C_键位映射配置::f_添加全局操作键位(S_键位映射* key, std::u16string name) {
	if (g_全局键位映射容器.find(name) == g_全局键位映射容器.end()) {
		S_Props prop = f_alloc_键位(key, name);

		g_全局键位映射容器[name] = prop;
		//f_ui_注册全局快捷键(key);
	}

	C_Widget::g_键位映射容器.insert(key);
}

void C_键位映射配置::f_添加3D视口操作键位(C_Widget* self, S_键位映射* key, std::u16string keyName, std::u16string name) {
	if (g_3D视口键位映射容器.find(keyName) == g_3D视口键位映射容器.end()) {
		S_Props prop = f_alloc_键位(key, name);

		g_3D视口键位映射容器[keyName] = prop;
		C_Widget::g_键位映射容器.insert(key);
	}
	else {
		std::cout<<"重复键值名称\n";
	}

	f_配置_添加操作键位(self, key);
}

void C_键位映射配置::f_添加2D视口操作键位(S_键位映射* key, std::u16string keyName, std::u16string name) {
	if (g_2D视口键位映射容器.find(keyName) == g_2D视口键位映射容器.end()) {
		S_Props prop = f_alloc_键位(key, name);

		g_2D视口键位映射容器[keyName] = prop;
	}

	C_Widget::g_键位映射容器.insert(key);
}

void C_键位映射配置::f_添加节点视口操作键位(C_Widget* self, S_键位映射* key, std::u16string keyName, std::u16string name) {
	if (g_节点视口键位映射容器.find(keyName) == g_节点视口键位映射容器.end()) {
		S_Props prop = f_alloc_键位(key, name);

		g_节点视口键位映射容器[keyName] = prop;
		C_Widget::g_键位映射容器.insert(key);
	}

	f_配置_添加操作键位(self, key);
}

void C_键位映射配置::f_添加曲线视口操作键位(C_Widget* self, S_键位映射* key, std::u16string keyName, std::u16string name) {
	if (g_曲线视口键位映射容器.find(keyName) == g_曲线视口键位映射容器.end()) {
		S_Props prop = f_alloc_键位(key, name);

		g_曲线视口键位映射容器[keyName] = prop;
		C_Widget::g_键位映射容器.insert(key);
	}

	f_配置_添加操作键位(self, key);
}

void C_键位映射配置::f_添加大纲视口操作键位(C_Widget* self, S_键位映射* key, std::u16string keyName, std::u16string name) {
	if (g_大纲视口键位映射容器.find(keyName) == g_大纲视口键位映射容器.end()) {
		S_Props prop = f_alloc_键位(key, name);

		g_大纲视口键位映射容器[keyName] = prop;
		C_Widget::g_键位映射容器.insert(key);
	}

	f_配置_添加操作键位(self, key);
}



void C_键位映射配置::f_清除快捷键配置() {
	g_全局键位映射容器.clear();
	g_3D视口键位映射容器.clear();
	g_2D视口键位映射容器.clear();
	g_节点视口键位映射容器.clear();
	g_曲线视口键位映射容器.clear();
	g_大纲视口键位映射容器.clear();
}

std::vector<std::u16string> C_键位映射配置::f_取3D视口快捷键名称字符(std::u16string name) {
	if(g_3D视口键位映射容器.find(name) == g_3D视口键位映射容器.end()) return {u""};

	const auto& prop = g_3D视口键位映射容器[name];
	name = g_3D视口键位映射容器[name].m_Name + u":";
	auto key = f_prop_键位(prop);

	
	auto 鼠标按键名称 = [](uint8 s) ->std::u16string {
		switch (s) {
			case E_Input_鼠标按键::e_左键: return u"鼠标左键";
			case E_Input_鼠标按键::e_右键: return u"鼠标右键";
			case E_Input_鼠标按键::e_中键: return u"鼠标中键";
			case E_Input_鼠标按键::e_滚轮: return u"鼠标滚轮";
			default:
				break;
		}
		return u"";
	};

	std::u16string 操作类型;
	std::u16string 键盘按键;
	std::u16string 鼠标按键;

	switch (key->m_Type) {
		case E_键位映射类型::e_键映射类型_键盘: {
			const S_键盘键位映射* k = (const S_键盘键位映射*)key;

			for (uint32 i = 0; i< 4; ++i) {
				if (k->键盘按键[i]) {
					if(!键盘按键.empty()) 键盘按键 += u" + ";
					键盘按键 += f_input_getDX键码名称(k->键盘按键[i]);
				}
			}

			操作类型 = u"键盘按键";
			break;
		}
		case E_键位映射类型::e_键映射类型_鼠标: {
			const S_鼠标键位映射* k = (const S_鼠标键位映射*)key;

			for (uint32 i = 0; i< 4; ++i) {
				if (k->键盘按键[i]) {
					if(!键盘按键.empty()) 键盘按键 += u" + ";
					键盘按键 += f_input_getDX键码名称(k->键盘按键[i]);
				}
			}
			for (uint32 i = 0; i< 4; ++i) {
				if (k->鼠标状态[i] || k->鼠标按键[i]) {
					if(!鼠标按键.empty()) 鼠标按键 += u" + ";
					鼠标按键 += 鼠标按键名称(k->鼠标按键[i]);
				}
			}
			
			操作类型 = u"鼠标按键";
			break;
		}
		case E_键位映射类型::e_键映射类型_双击: {
			break;
		}
		case E_键位映射类型::e_键映射类型_拖拽: 
		case E_键位映射类型::e_键映射类型_拖放: {
			const S_拖拽键位映射* k = (const S_拖拽键位映射*)key;

			
			for (uint32 i = 0; i< 4; ++i) {
				if (k->键盘按键[i]) {
					if(!键盘按键.empty()) 键盘按键 += u" + ";
					键盘按键 += f_input_getDX键码名称(k->键盘按键[i]);
				}
			}
			for (uint32 i = 0; i< 4; ++i) {
				if (k->鼠标状态[i] || k->鼠标按键[i]) {
					if(!鼠标按键.empty()) 鼠标按键 += u" + ";
					鼠标按键 += 鼠标按键名称(k->鼠标按键[i]);
				}
			}
			
			操作类型 = u"键鼠拖拽";
			break;
		}
		default:
			break;
	}

	if (鼠标按键.empty() == false) {
		if (键盘按键.empty() == false) 键盘按键 += u" + ";
		键盘按键 += 鼠标按键;
	}
	

	return {name, 操作类型, 键盘按键};
}








